package db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by chenj
 * Created on 2017/8/17
 */

public class DBHelper extends SQLiteOpenHelper {

    private final static String[] mDbName = {"DB_0", "DB_1", "DB_2", "DB_3", "DB_4", "DB_5", "DB_6", "DB_7", "DB_8", "DB_9", "DB_A", "DB_B", "DB_C", "DB_D", "DB_E", "DB_F", "DB_G", "DB_H", "DB_I", "DB_J", "DB_K", "DB_L", "DB_M", "DB_N", "DB_O", "DB_P", "DB_Q", "DB_R", "DB_S", "DB_T", "DB_U", "DB_V", "DB_W", "DB_X", "DB_Y", "DB_Z"};
    public static final String COLUMN_FUND_ID = "suggest_text_2";

    public static final String COLUMN_FUND_NAME = "suggest_text_1";

    public static final String COLUMN_FUND_TYPE = "fundType";

    public static final String COLUMN_INVST_TYPE = "invstType";

    public static final String COLUMN_INVST_STYLE = "invstStyle";

    public static final String COLUMN_INVST_QUAL = "invstQual";

    public static final String COLUMN_ESTAB_DATE = "estabDate";

    public static final String COLUMN_FUND_PINYIN = "fundPinyin";

    public static final String COLUMN_RISK_LEVEL = "riskLevel";

    // 最小认购金额
    public static final String COLUMN_MIN_SUB_AMOUNT = "minSubAmount";

    // 最小申购金额
    public static final String COLUMN_MIN_BID_AMOUNT = "minBidAmount";

    // 最小定投金额
    public static final String COLUMN_MIN_MIP_AMOUNT = "minMipAmount";

    // 最小赎回份额
    public static final String COLUMN_MIN_RED_QTY = "minRedQty";

    // 众禄类型
    public static final String COLUMN_FUND_ZL_TYPE = "zlType";

    public static final String COLUMN_FUND_CATEGORY = "fundcategory";

    // 预留字段
    public static final String COLUMN_FUND_UNKNOWN_1 = "unknown1";

    public static final String COLUMN_FUND_UNKNOWN_2 = "unknown2";

    public static final String COLUMN_FUND_UNKNOWN_3 = "unknown3";

    public static final String COLUMN_FUND_UNKNOWN_4 = "unknown4";

    public static final String COLUMN_FUND_UNKNOWN_5 = "unknown5";

    public static final String COLUMN_FUND_UNKNOWN_6 = "unknown6";

    public static final String COLUMN_FUND_UNKNOWN_7 = "unknown7";

    public static final String COLUMN_FUND_UNKNOWN_8 = "unknown8";

    // fields not belong to table
    public static final String COLUMN_TRADE_ACCO = "tradeacco";

    public static final String COLUMN_PAY_NAME = "payname";

    public static final String COLUMN_PAY_ST = "payst";

    public static final String COLUMN_SUB_AMOUNT = "subamt";

    public static final String COLUMN_RED_QTY = "redqty";

    public static final String COLUMN_MIP_CYCLE = "mipCycle";

    public static final String COLUMN_MIP_FIRST_DATE = "mipFirstDate";

    public static final String COLUMN_FUND_FAVORITED = "fundFavorited";

    public static final String COLUMN_SERIAL_NO = "serialno";

    public static final String COLUMN_FROM_FUND_ID = "fromFundId";

    public static final String COLUMN_IS_NEW_FUND = "isNewFund";
    public static final String COLUMN_TRADE_APKIND = "apkind";
    private String[] mAllData;

    public DBHelper(Context context) {
        this(context, DBConstant.DB_NAME);
    }

    public DBHelper(Context context,String name){
        super(context, name, null, DBConstant.DB_VERSION);
        mAllData = new String[]{
                COLUMN_FUND_ID, COLUMN_FUND_NAME, COLUMN_FUND_TYPE, COLUMN_FUND_CATEGORY,
                COLUMN_INVST_TYPE, COLUMN_INVST_STYLE, COLUMN_INVST_QUAL, COLUMN_ESTAB_DATE,
                COLUMN_FUND_PINYIN, COLUMN_RISK_LEVEL, COLUMN_MIN_SUB_AMOUNT,
                COLUMN_MIN_BID_AMOUNT, COLUMN_MIN_MIP_AMOUNT, COLUMN_MIN_RED_QTY,
                COLUMN_FUND_ZL_TYPE, COLUMN_FUND_UNKNOWN_1, COLUMN_FUND_UNKNOWN_2,
                COLUMN_FUND_UNKNOWN_3, COLUMN_FUND_UNKNOWN_4, COLUMN_FUND_UNKNOWN_5,
                COLUMN_FUND_UNKNOWN_6, COLUMN_FUND_UNKNOWN_7, COLUMN_FUND_UNKNOWN_8
        };
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        for (int i = 0; i < mDbName.length; i++) {
            db.execSQL(createExecSql(mDbName[i]));
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public String createExecSql(String name) {
        return "CREATE VIRTUAL TABLE " + name + " USING fts3 ("
                + COLUMN_FUND_ID + ", " + COLUMN_FUND_NAME + ", " + COLUMN_FUND_TYPE + ", " + COLUMN_FUND_CATEGORY + ", "
                + COLUMN_INVST_TYPE + ", " + COLUMN_INVST_STYLE + ", " + COLUMN_INVST_QUAL + ", " + COLUMN_ESTAB_DATE
                + ", " + COLUMN_FUND_PINYIN + ", " + COLUMN_RISK_LEVEL + ", " + COLUMN_MIN_SUB_AMOUNT + ", "
                + COLUMN_MIN_BID_AMOUNT + ", " + COLUMN_MIN_MIP_AMOUNT + ", " + COLUMN_MIN_RED_QTY + ", "
                + COLUMN_FUND_ZL_TYPE + ", " + COLUMN_FUND_UNKNOWN_1 + ", " + COLUMN_FUND_UNKNOWN_2 + ", "
                + COLUMN_FUND_UNKNOWN_3 + ", " + COLUMN_FUND_UNKNOWN_4 + ", " + COLUMN_FUND_UNKNOWN_5 + ", "
                + COLUMN_FUND_UNKNOWN_6 + ", " + COLUMN_FUND_UNKNOWN_7 + ", " + COLUMN_FUND_UNKNOWN_8 + ");";
    }

    public Cursor query(SQLiteDatabase db,String[] args) {
//        db.query()
        return db.query(DBConstant.TABLE_NAME,args,COLUMN_FUND_ID + " = ?",new String[]{"S72525"},null,null,null);
    }

    public Cursor queryAll(SQLiteDatabase db,String selection,String[] selectArgs){
        return db.query(DBConstant.TABLE_NAME,mAllData,selection,selectArgs,null,null,null);
    }
}
